// pages/Information/Information.js
var network = require("../../api/api.js")
var util = require('../../utils/util')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPass: true,
    front_back_img: '../../images/in1.png',
    back_back_img: '../../images/inde3.png',
    getProfile: {},
    editCard: {
      real_name: '',
      id_card: '',
      // front_id_card: '',
      // back_id_card: ''
    },
    disabled: false
  },
  infoInput: function (e) {
    const key = e.target.dataset.key
    const data = new Object()
    data[key] = e.detail.value
    this.setData({
      getProfile: Object.assign({}, this.data.getProfile, data)
    })
    this.setData({
      editCard: Object.assign({}, this.data.editCard, data)
    })
  },

  chooseImage: function (e) {
    var that = this
    const key = e.currentTarget.dataset.key
    const url = e.currentTarget.dataset.url
    app.globalData.leave = 1
    let type = key == 'front_id_card' ? '2' : '3';
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        app.globalData.leave = 0
        that.updateImg(res.tempFilePaths[0], type)
        // wx.showLoading({
        //   title: '文件加载中',
        //   mask: true
        // })
        // util.canvasToBase.call(that, res.tempFilePaths[0], {
        //   width: 358,
        //   height: 441
        // }, function (base64) {
        //   const data = new Object()
        //   data[url] = res.tempFilePaths[0]
        //   that.setData({
        //     getProfile: Object.assign({}, that.data.getProfile, data)
        //   })
        //   const baseData = new Object()
        //   baseData[key] = base64
        //   that.setData({
        //     editCard: Object.assign({}, that.data.editCard, baseData)
        //   })
        //   wx.hideLoading()
        // })
      }
    })
  },

  confirm_upload: function () {
    var that = this
    var userLogin = wx.getStorageSync('user_login') || {}
    const editCard = this.data.editCard
    const keys = Object.keys(editCard)
    const result = keys.every(v => {
      return !!editCard[v]
    })
    if (!result) {
      // 检查是否还有未填写的项
      wx.showToast({
        title: '检查是否还有未填写的项',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var params = new Object()
    editCard.image_suffix = 'png'
    editCard.user_id = String(userLogin.user_id)
    params.business_param = editCard
    network.POST({
      params: params,
      url: "edit_user_idcard_img",
      success: function (res) {
        console.log(res);
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
          upload_card: {
            real_name: that.data.editCard.real_name,
            id_card: that.data.editCard.id_card
          }
        })
        wx.navigateBack({
          delta: 1,
        })
        //拿到解密后的数据，进行代码逻辑

      },
      fail: function () {
        //失败后的逻辑

      },
    })
  },
  updateImg: function (path, type) {
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    })
    var userLogin = wx.getStorageSync('user_login') || {}
    var that = this;
    wx.uploadFile({
      url: network.UPLOAD_URL, //仅为示例，非真实的接口地址
      filePath: path,
      name: 'Key',
      header: {
        "Content-Type": "multipart/form-data"
      },
      formData: {
        "type": String(type) || '2',
        "user_id": String(userLogin.user_id)
      },
      success: function (res) {
        var data = res.data
        if (typeof data === 'string') {
          data = JSON.parse(data)
        }
        that.setData({
          getProfile: Object.assign({}, that.data.getProfile, {
            id_card_front: data.result[0].id_card_front,
            id_card_back: data.result[0].id_card_back
          })
        })
        wx.hideLoading()
      },
      fail: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var userLogin = wx.getStorageSync('user_login') || {}
    const userInfo = JSON.parse(options.getProfile)
    this.setData({
      isPass: userLogin.audit_status === 2
    })
    var params = new Object()
    params.business_param = {
      user_id: String(userLogin.user_id)
    }
    network.POST({
      params: params,
      url: "get_user_info",
      success: function (res) {
        const result = res.result[0] || {}
        that.setData({
          getProfile: Object.assign({}, result, userInfo)
        })
        that.setData({
          editCard: Object.assign({}, that.data.editCard, {
            real_name: result.real_name
          })
        })
        that.setData({
          editCard: Object.assign({}, that.data.editCard, {
            id_card: result.id_card
          })
        })
        if (result.id_card_front || result.id_card_back) {
          // baseData['back_id_card'] = result.id_card_front
          // baseData['front_id_card'] = result.id_card_back
          // wx.showLoading({
          //     title: '加载中',
          //     mask: true
          // })
          let add = 0
          // util.canvasToBase.call(that, result.id_card_front, {
          //   width: 358,
          //   height: 441
          // }, function (base64) {
          //   const baseData = new Object()
          //   baseData['front_id_card'] = base64
          //   that.setData({
          //       editCard: Object.assign({}, that.data.editCard, baseData)
          //     })
          //     ++add
          //   if (add === 2) {
          //     wx.hideLoading()
          //   }
          // })
          // util.canvasToBase.call(that, result.id_card_back, {
          //   width: 358,
          //   height: 441
          // }, function (base64) {
          //   const baseData = new Object()
          //   baseData['back_id_card'] = base64
          //   console.log(base64)
          //   that.setData({
          //       editCard: Object.assign({}, that.data.editCard, baseData)
          //     })
          //     ++add
          //   if (add === 2) {
          //     wx.hideLoading()
          //   }
          // })
        }
        //拿到解密后的数据，进行代码逻辑

      },
      fail: function () {
        //失败后的逻辑

      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})